package com.easy.web.controller;

import com.easy.web.model.Review;
import com.easy.web.service.ReviewService;
import com.easy.web.utils.StringUtil;
import com.easy.web.utils.ViewUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.UUID;

/**
 * Created by wang0 on 2017/8/24.
 */
@Controller
@RequestMapping("review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;


    /**
     * 获取老师列表
     */
    @RequestMapping(value = "getReviewList", method = {RequestMethod.GET})
    public ModelAndView getReviewList() {
        List<Review> reviewList = reviewService.getReviewList();

        return new ModelAndView("", ViewUtils.mapSuccess(reviewList));
    }


    /**
     * 新增一个老师
     */
    @RequestMapping(value = "addReview", method = {RequestMethod.POST})
    public ModelAndView addReview(@RequestParam(value = "baseValue", required = true) String baseValue) {

        JSONObject arJson = JSONObject.fromObject(baseValue);
        Review review = new Review();
        if (!arJson.isEmpty()) {
            review.setId(UUID.randomUUID().toString());
            review.setName(arJson.optString("setReviewName"));
            review.setHeadImg(arJson.optString("setReviewTime"));
            review.setOperationTime(arJson.optString("setReviewImg"));
            review.setContent(arJson.optString("setReviewContent"));
            String floor=arJson.optString("setReviewFloor");
            if(StringUtil.isNumeric(floor)){
                Review reviewTemp=reviewService.queryReviewByFloor(floor);
                if(reviewTemp==null){
                    review.setFloor(floor);
                }else {
                    return  new ModelAndView("", ViewUtils.mapFail("该楼层已存在！"));
                }
            }else {
                return new ModelAndView("", ViewUtils.mapFail("请输入正确的正整数"));
            }
        }
        try {
            reviewService.addReview(review);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("", ViewUtils.mapFail("新增失败"));
        }

        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 删除一个老师
     */
    @RequestMapping(value = "delReview", method = {RequestMethod.POST})
    public ModelAndView delReview(@RequestParam(value = "delReviewUid", required = true) String id) {

        reviewService.delReview(id);
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }


    /**
     * 更新一个老师信息
     */
    @RequestMapping(value = "updateReview", method = {RequestMethod.POST})
    public ModelAndView updateReview(@RequestParam(value = "baseValue", required = true) String baseValue) {

        JSONObject arJson = JSONObject.fromObject(baseValue);
        if (!arJson.isEmpty()) {
            Review review = reviewService.queryReviewById(arJson.optString("editReviewUid"));
            if (review == null) {
                return new ModelAndView("", ViewUtils.mapFail("编辑的该留言不存在，请重新编辑"));
            } else {
                review.setName(arJson.optString("editReviewName"));
//                review.setFloor(arJson.optString("editReviewFloor"));
                review.setHeadImg(arJson.optString("editReviewTime"));
                review.setOperationTime(arJson.optString("editReviewImg"));
                review.setContent(arJson.optString("editReviewContent"));
                String floor=arJson.optString("editReviewFloor");

                if(!review.getFloor().equals(floor)) {
                    if (StringUtil.isNumeric(floor)) {
                        Review reviewTemp = reviewService.queryReviewByFloor(floor);
                        if (reviewTemp == null) {
                            review.setFloor(floor);
                        } else {
                            return new ModelAndView("", ViewUtils.mapFail("该楼层已存在！"));
                        }
                    } else {
                        return new ModelAndView("", ViewUtils.mapFail("请输入正确的正整数"));
                    }
                }
                try {
                    reviewService.updateReview(review);
                } catch (Exception e) {
                    e.printStackTrace();
                    return new ModelAndView("", ViewUtils.mapFail("更新失败"));
                }
            }

        }


        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 根据id查询文章
     */
    @RequestMapping(value = "queryReview", method = {RequestMethod.GET})
    public ModelAndView queryReview(@RequestParam(value = "editReviewUid", required = true) String id) {
        Review review = reviewService.queryReviewById(id);
        return new ModelAndView("", ViewUtils.mapSuccess(review));
    }
}
